#include <stdio.h>

unsigned short checksum(unsigned short *pdate, int size) {
	unsigned int sum = 0;
	while (size > 1) {
		sum += *pdate;
		pdate++;
		size -= sizeof(short);
	}
	if (size)
		sum += *(unsigned char*)pdate;
	while (sum >> 16) {
		sum = (sum & 0xffff) + (sum >> 16);
	}
	return ~sum;
}

int main() {
	unsigned short checksum_code = 0;
	unsigned short date[] = {0, 0x4500, 0x0029, 0x44f1, 0x4000, 0x8006, 0xc0a8, 0x01ae, 0x4a7d, 0x477d}; //date[0]存放校验码
	unsigned short check_res = 0;

	checksum_code = checksum((unsigned short*)date, sizeof(date));//产生校验码
	date[0] = checksum_code;
	printf("generate:%x\n", checksum_code);

	check_res = checksum((unsigned short*)date, sizeof(date)); //校验结果
	printf("check result:%x\n", check_res);
	return 0;
}
